package com.ce.pms.charge.dao.impl;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IPeriodDao;
import com.ce.pms.charge.model.PeriodDomain;
import com.ce.pms.common.annotation.AssociatedPrecinctQuery;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.util.BlankUtil;

public class PeriodDaoImpl extends PmsBaseDaoImpl<PeriodDomain> implements IPeriodDao {
	/**
	 * 方法描述: 根据楼栋查询当前有效的收费周期实体
	 * @author 胡煦
	 * Create on:2008-8-18
	 * @param
	 * @return PeriodDomain
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PeriodDomain findAvailPeriodBy(Long buildingId){
		 String queryStr =  "SELECT * FROM FEE_PERIOD pd left outer join HM_CHARGE_PRECINCT_R_BUILDING cb ON pd.CHARGE_PRECINCT_ID = cb.CHARGE_PRECINCT_ID WHERE cb.BUILDING_ID = ? and pd.validity = '1'";
		 Query query = this.createSQLQuery(queryStr,new Object[]{buildingId});
		 List list = query.list();
		 if(BlankUtil.isBlank(list)){
			 return null;
		 }
		 Object[] object = (Object[]) list.get(0);
		 BigInteger l = (BigInteger) object[0];
		 return this.findById(l.longValue());
	}


	/**
	 * 功能描述:取得当前收费周期，与权限配合使用才有实际意义<br>
	 * @param houseId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-16<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	@AssociatedPrecinctQuery(alias="pd", beginStr=" AND ", endStr=" ", insertAfterPoint="WHERE 1=1",property4Precinct="chargePrecinct.keyword")
	public PeriodDomain queryCurrentPeriod(){
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT pd FROM PeriodDomain pd WHERE 1=1 AND pd.validity='1'");
		Query query=this.createQuery(sql.toString(), null);
		query.setFirstResult(0);
		query.setMaxResults(1);
		Object result=query.uniqueResult();
		if(result!=null){
			return (PeriodDomain)result;
		}else{
			return null;
		}
	}

	/**
	 * 功能描述:根据收费周期ID查询收费周期<br>
	 * @param ids
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<PeriodDomain> queryPeriodByIds(Set<Long> ids){
		StringBuffer pids = getIds(ids);
		String sql="SELECT pd FROM PeriodDomain pd WHERE  pd.validity='1' AND pd.keyword in("+pids+")";
		Query query=this.createQuery(sql,null);
		List<PeriodDomain> list=query.list();
		return list;
	}


	/**
	 * 功能描述:<br>
	 * @param ids
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private StringBuffer getIds(Set<Long> ids) {
		StringBuffer pids=new StringBuffer();
		for(Long id : ids){
			if(id!=null){
				pids.append(id).append(",");
			}
		}
		if(pids!=null && pids.length()>0 && pids.charAt(pids.length()-1)==','){
			pids.deleteCharAt(pids.length()-1);
		}
		return pids;
	}


	/**
	 * 功能描述:根据收费周期状态查询收费周期   <br>
	 * @param validity 0:无效(已月结,按收费周期分组)  1:有效(未月结不分组) null:所有的收费周期，并以收费周期分组
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	@AssociatedPrecinctQuery(alias="per", beginStr=" AND ", endStr=" ", insertAfterPoint="WHERE 1=1",property4Precinct="chargePrecinct.keyword")
	public List<PeriodDomain> findPeriodByValidity(String validity) {
		StringBuffer queryStr = new StringBuffer();
		queryStr.append(" FROM PeriodDomain per WHERE 1=1 ") ;
		if(!BlankUtil.isBlank(validity)){
			queryStr.append("  and per.validity = "+ validity  ) ;
			//如果查询无效的收费周期时对收费周期进行分组
			if (validity.equals(SysrefcodeDO.FEE_PERIOD_VALIDITY_INVALIDATION)){
				queryStr.append(" group by per.period ");
			}
			queryStr.append(" order by per.period desc");
		}//如果为空时查询所有的收费周期，并且按照收费周期公组
		else{
			queryStr.append(" group by per.period  order by per.period desc");
		}

		Query query = this.createQuery(queryStr.toString(), null);
		List<PeriodDomain> list = query.list();
		return list;
	}

	/**
	 * 功能描述:根据收费管理区查询收费周期   <br>
	 * @param PrecinctId  管理区ID
	 * @param validity  收费周期有效性 0:无效  1:有效  null:查询全部
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<PeriodDomain> findPeriodByPrecinct(Long PrecinctId, String validity) {
		if (BlankUtil.isBlank(PrecinctId)){
			return null;
		}

		StringBuffer queryStr = new StringBuffer();
		queryStr.append(" FROM PeriodDomain per ") ;
		queryStr.append("  where per.chargePrecinct.keyword = ? ") ;
		//如果不为空时则按条件查询,为空时则查询全部
		if(!BlankUtil.isBlank(validity)){
			queryStr.append("  and per.validity = ?  ");
		}
		queryStr.append("  order by per.period desc");

		Object [] obj = null ;
		if (BlankUtil.isBlank(validity)){
			obj = new Object[1];
			obj[0] = PrecinctId;
		}else{
			obj = new Object[2];
			obj[0] = PrecinctId;
			obj[1] = validity;
		}
		Query query = this.createQuery(queryStr.toString(), obj);
		List<PeriodDomain> list = query.list();
		return list;
	}
	/**
	 * 方法描述: 根据收费收费管理区ID和收费周期名称查询实体方法
	 * @author 胡煦
	 * Create on:2008-9-26
	 * @param chargePrecinctId，period
	 * @return PeriodDomain
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PeriodDomain findPeriodByName(Long chargePrecinctId,String period){
		 String hql =  "from PeriodDomain pd where pd.chargePrecinct.id = ? and pd.period = ?";
		 if(BlankUtil.isBlank(period)){
			 return null;
		 }
		 Query query = createQuery(hql, new Object[]{chargePrecinctId,period});
		 return  query.list().size()==0?null:(PeriodDomain) query.list().get(0);
	}



}
